import { Injectable } from '@angular/core';
import { HeroService } from './hero.service';
import { NzModalService } from 'ng-zorro-antd';
import { Router } from '@angular/router';

@Injectable({
  providedIn: 'root',
})
export class LoginguardService {
  
  public aa:boolean;
  constructor(private heroService: HeroService,
              private router: Router,
              private modalService: NzModalService) {}

  canActivate(): boolean {
    let token=localStorage.getItem('token'); 
    if(token){
      return true;
    }else{
      return false;
    }
  }

  canActivateChild(): Promise<boolean> {
    // 判断token 是否失效
    let url=host+"/main/info";
    return this.heroService.get(url).then(result =>{
      if(result.code == 0){
        return true;
      }else{
        this.modalService.confirm({
          nzTitle: '提示',
          nzContent: '<b>登录超时，请重新登录?</b>',
          nzOnOk: () => this.router.navigate(['/login'])
        });
        return false;
      }
    });
  }
}